package com.zxy.supplier_system.server.utils.wechat.official

import java.time.Duration

private const val key = "accessToken"
fun WechatOfficialAccountHelper.getOrFetchAccessToken(): String {
    val value = this.storage[key]
    if (value == null) {
        val responseEntity = this.getAccessToken()
        val getAccessTokenResponse = responseEntity.body!!
        val accessToken = getAccessTokenResponse.access_token!!
        storage.put(key, accessToken, Duration.ofSeconds(getAccessTokenResponse.expires_in!!.toLong()))
        return accessToken
    }
    return value
}
